package cn.com.nxt.txb.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import cn.com.nxt.txb.R;

/**
 * 加强版的EditText,可以响应DrawableLeft 和 DrawableRight的点击事件
 * 要实现响应点击,先设置setDrawableListener
 * @author xing
 * @version 1.1
 */
public class XEditText extends EditText  implements TextWatcher {
	/**
	 * 删除按钮的引用
	 */
	private Drawable mClearDrawable;
	private DrawableLeftListener mLeftListener ;
	private DrawableRightListener mRightListener ;
    final int DRAWABLE_RIGHT = 2;

	@SuppressLint("NewApi")
	public XEditText(Context context, AttributeSet attrs, int defStyleAttr,
                     int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
	}

	public XEditText(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public XEditText(Context context, AttributeSet attrs) {
		//super(context, attrs);
		this(context, attrs, android.R.attr.editTextStyle);

	}

	public XEditText(Context context) {
		super(context);
	}

	public void setDrawableLeftListener(DrawableLeftListener listener) {
		this.mLeftListener = listener;
	}

	public void setDrawableRightListener(DrawableRightListener listener) {
		this.mRightListener = listener;
	}

	/**
	 * 设置清除图标的显示与隐藏，调用setCompoundDrawables为EditText绘制上去
	 * @param visible
	 */
	protected void setClearIconVisible(boolean visible) {
		Drawable right = visible ? mClearDrawable : null;
		setCompoundDrawables(getCompoundDrawables()[0],
				getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
	}

	private void init() {
		//获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
		mClearDrawable = getCompoundDrawables()[2];
		if (mClearDrawable == null) {
//          throw new NullPointerException("You can add drawableRight attribute in XML");
			mClearDrawable = getResources().getDrawable(R.drawable.ic_bk);
		}

		mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
		//默认设置隐藏图标
		//setClearIconVisible(false);
		//设置焦点改变的监听
		//setOnFocusChangeListener(this);
		//设置输入框里面内容发生改变的监听
		addTextChangedListener(this);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		setClearIconVisible(s.length() > 0);
	}

	@Override
	public void afterTextChanged(Editable s) {

	}

	public interface DrawableLeftListener {
		public void onDrawableLeftClick(View view) ;
	}
	
	public interface DrawableRightListener {
		public void onDrawableRightClick(View view) ;
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			if (mRightListener != null) {
				Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT] ;
				if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width())) {
					mRightListener.onDrawableRightClick(this) ;
	        		return true ;
				}
			}
			break;
		}
		
		return super.onTouchEvent(event);
	}
}